package com.mobile.web;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SecurityFilter implements Filter {
	
	protected Log log = LogFactory.getLog(this.getClass());

	public void destroy() {}
	public void init(FilterConfig arg0) throws ServletException {}
	
	public void doFilter(ServletRequest reqServlet, ServletResponse resServlet, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)reqServlet;
		
        if (log.isDebugEnabled()) log.debug("%%%%%%%%%%%%%%%%% URI:>>>"+req.getRequestURI()+"<<<");
		Enumeration<String> e = req.getParameterNames();
		while(e.hasMoreElements()){
			String x = e.nextElement();
			if (log.isDebugEnabled()) log.debug("%%%%%%%%%%%%%%%%%PARAM:"+x+":>>>"+req.getParameter(x)+"<<<");
		}		
		Enumeration<String> attrs = req.getAttributeNames();
		while(attrs.hasMoreElements()){
			String x = attrs.nextElement();
			if (log.isDebugEnabled()) log.debug("%%%%%%%%%%%%%%%%%ATTR:"+x+":>>>"+req.getAttribute(x)+"<<<");
		}
		
		try{
			chain.doFilter(reqServlet, resServlet);
		} catch(Exception ex){
			HttpServletResponse res = (HttpServletResponse)resServlet;
			res.getWriter().print("Erro: " + ex.getMessage());
		}
	}
}